<!--
 * @Author: your name
 * @Date: 2021-12-06 08:40:49
 * @LastEditTime: 2021-12-08 20:12:04
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \demo\src\views\RentSearch\index.vue
-->
<template>
  <div>
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入小区或地址"
        @search="onSearch"
        @cancel="onCancel"
      />
    </form>

    <div>
      <van-cell
        :title="item"
        v-for="item in list"
        :key="item"
        @click="onValue(item)"
      />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Toast } from 'vant'
import { getAreaCommunity } from '@/api/area.js'
export default {
  data () {
    return {
      value: '',
      list: []
    }
  },

  created () {},
  computed: {
    ...mapState(['cityId'])
  },

  methods: {
    async onSearch (val) {
      this.list = []
      Toast(val)
      console.log(val)
      try {
        const res = await getAreaCommunity({
          name: val,
          id: this.cityId
        })
        console.log(res)
        res.data.body.forEach((item) => {
          this.list.push(item.communityName)
        })
        console.log(this.list)
      } catch (error) {
        Toast('获取小区失败')
      }
    },
    onCancel () {
      this.$router.push('/rent/add')
    },
    onValue (e) {
      console.log(e)
    }
  }
}
</script>

<style scoped lang="less"></style>
